<template>
  <div class="register">
    <div class="box">
      <div class="title">
        <div class="left">
          <div class="img" />
          <span>黑马面</span>
        </div>
        <div class="right">
          <span>用户注册</span>
        </div>
      </div>
      <div class="form">
        <el-form
          ref="dataForm"
          :model="dataForm"
          :rules="rules"
          label-width="70px"
        >
          <el-row>
            <el-col :span="13">
              <el-form-item prop="username" label="昵称">
                <el-input v-model="dataForm.username" />
              </el-form-item>
              <el-form-item prop="email" label="邮箱">
                <el-input v-model="dataForm.email" />
              </el-form-item>
              <el-form-item prop="phone" label="手机">
                <el-input v-model="dataForm.phone" />
              </el-form-item>
              <el-form-item prop="password" label="密码">
                <el-input
                  v-model="dataForm.password"
                  show-password
                  auto-complete="new-password"
                />
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item prop="avatar" label-width="20px" title="用户头像">
                <upload-img v-model="imgUrl" :http-request="httpRequest" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-form-item prop="code" label="图形码">
            <el-input v-model="dataForm.code" style="height: 45px" />
            <img
              v-if="bol"
              :src="baseUrl + 'captcha?type=sendsms'"
              alt=""
              style="margin-left: 20px; width: 140px"
              @click="refreshImg"
            >
          </el-form-item>
          <el-form-item prop="rcode" label="验证码">
            <el-input v-model="dataForm.rcode" style="height: 45px" />
            <el-button
              style="height: 40px; margin-left: 20px"
              :disabled="disabled"
              @click="sendSMS"
            >获取用户验证码<span v-if="show">{{
              '(' + time + 's' + ')'
            }}</span></el-button>
          </el-form-item>
          <el-form-item prop="checked" label-width="0">
            <el-checkbox
              v-model="dataForm.checked"
            >我已阅读并同意<span>用户协议</span>和<span>隐私条款</span></el-checkbox>
            <!-- <a @click="$router.push('/register')">注册账号</a> -->
            <div style="margin-left: 20px">
              <div>
                已有账号?前往<router-link
                  class="login"
                  :to="{ path: '/login' }"
                  tag="a"
                >登陆账号</router-link>
              </div>
            </div>
          </el-form-item>
        </el-form>
        <el-button
          type="primary"
          style="width: 100%; height: 40px"
          @click="submit"
        >注册</el-button>
      </div>
    </div>
    <div class="bigImg" />
  </div>
</template>
<script>
export default {
  name: '',
  data() {
    return {
      bol: true,
      imgUrl: '',
      time: 3,
      show: false,
      disabled: false,
      baseUrl: process.env.VUE_APP_URL,
      dataForm: {
        avatar: '',
        username: '',
        email: '',
        phone: '',
        password: '',
        code: '',
        rcode: ''
      },
      rules: {
        phone: [
          { required: true, message: '请输入手机号', trigger: 'change' },
          {
            pattern:
              /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '请输入正确的手机号',
            trigger: 'change'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'change' },
          { min: 6, max: 12, message: '请输入密码', trigger: 'change' }
        ],
        code: [
          { required: true, message: '请输入图形码', trigger: 'change' },
          { max: 4, min: 4, message: '请输入图形码', trigger: 'change' }
        ],
        rcode: [
          { required: true, message: '请输入验证码', trigger: 'change' },
          { max: 4, min: 4, message: '请输入验证码', trigger: 'change' }
        ],
        avatar: [{ required: true, message: '请上传头像', trigger: 'change' }],
        username: [
          { required: true, message: '请输入用户名', trigger: 'change' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'change' },
          {
            pattern: /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/,
            message: '请输入正确的邮箱',
            trigger: 'change'
          }
        ],
        checked: [
          {
            validator: (rule, value, callback) => {
              value === true ? callback() : callback(new Error('请勾选协议'))
            }
          }
        ]
      }
    }
  },
  create() {},
  methods: {
    refreshImg() {
      this.bol = false
      this.$nextTick(() => {
        this.bol = true
      })
    },
    async httpRequest(res) {
      const formData = new FormData()
      formData.append('image', res.file)
      const result = await this.$api.register.uploadsPOST(formData)
      const url = this.baseApi + result.data.file_path
      this.imgUrl = url
      this.dataForm.avatar = result.data.file_path
    },
    async sendSMS() {
      this.disabled = true
      this.show = true
      const interval = setInterval(() => {
        this.time--
        if (this.time <= 0) {
          this.disabled = false
          this.time = 3
          this.show = false
          clearInterval(interval)
        }
      }, 1000)
      const res = await this.$api.register.sendSMS({
        code: this.dataForm.code,
        phone: this.dataForm.phone
      })
      this.$message.success(String(res.data.captcha))
    },
    async submit() {
      this.$refs.dataForm.validate(async(result) => {
        if (result) {
          const res = await this.$api.register.registerPOST(this.dataForm)
          this.$message.success('注册成功')
          this.$router.push('/login')
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.register {
  background-color: #119bfa;
  min-height: 100vh;
  overflow: hidden;
  display: flex;
  .bigImg {
    position: absolute;
    right: 73px;
    top: 234px;
    width: 633px;
    height: 435px;
    background-image: url(/static/img/login_banner_ele.c304439e.png);
    background-position: center right;
    background-size: 100%;
    background-repeat: no-repeat;
  }
  .box {
    margin-top: 80px;
    margin-left: 300px;
    background-color: #fff;
    width: 660px;
    height: 660px;
    padding: 48px;
    z-index: 9999;
    .title {
      display: flex;
      .left {
        display: flex;
        align-items: center;
        border-right: 1px solid #ccc;
        .img {
          margin-right: 16px;
          width: 22px;
          height: 17px;
          background: url('~@/assets/common/login_logo.png') center center /
            cover;
        }
        span {
          margin-right: 14px;
        }
      }
      .right {
        flex: 1;
        margin-left: 11px;
      }
      font-size: 24px;
      margin-bottom: 27px;
    }
    .form {
      width: 564px;
      span {
        color: #409eff;
        &:hover {
          border-bottom: 1px solid #409eff;
        }
      }
      .login {
        color: #119bfa;
        margin-left: 10px;
      }
      .avatar-uploader ::v-deep .el-upload {
        border: 1px dashed #d9d9d9;

        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
      }
      .avatar-uploader ::v-deep .el-upload:hover {
        border-color: #409eff;
      }
      .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 220px;
        height: 220px;
        line-height: 220px;
        text-align: center;
      }
      ::v-deep {
        .img-wrap,
        .img-wrap .btn-wrap {
          width: 220px;
          height: 220px;
          .el-icon-zoom-in {
            display: none;
          }
        }
        .el-icon-plus {
          width: 220px;
          line-height: 220px;
        }
        .avatar {
          width: 220px;
          height: 220px;
          object-fit: cover;
          display: block;
        }
        .el-form-item__content {
          display: flex;
        }
      }
    }
    ::v-deep .el-input {
      width: 230px;
    }
  }
}
</style>
